package cn.yywd_123.akarin.server.utils;

/*
 *  Author: yywd_123
 *  Date: 2022/11/21
 */

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class UserManager {
	private final Map<String, SocketServer.ClientHandler> userMap = new ConcurrentHashMap<>();
	private final Map<String, Long> userMuteMap = new ConcurrentHashMap<>();
	private final List<String> bannedUserList = new CopyOnWriteArrayList<>();
	private final List<String> bannedIpList = new CopyOnWriteArrayList<>();

	public void addUser(String userName, SocketServer.ClientHandler client) {
		if (userName.equals("Server")) {
			client.send("用户名已存在!");
			return;
		}
		if (!userMap.containsKey(userName) && !userMap.containsValue(client)) {
			userMap.put(userName, client);
		} else client.send("用户名已存在!");
	}

	public void removeUser(String userName) {
		userMap.remove(userName);
	}

	public void muteUser(String userName, String time) {
		SocketServer.ClientHandler client = userMap.get(userName);
		if (client == null) {
			System.err.println("未找到用户");
			return;
		}
		if (time == null || time.isEmpty()) {
			userMuteMap.remove(userName);
		} else {
			long i;
			try {
				i = Integer.parseInt(time.substring(0, time.length() - 1));
			} catch (NumberFormatException e) {
				System.err.println("无效的时间!");
				return;
			}
			long t;
			switch (time.charAt(time.length() - 1)) {
				case 'H':
				case 'h': t = TimeUnit.HOURS.toNanos(i);
					break;
				case 'M':
				case 'm': t = TimeUnit.MINUTES.toNanos(i);
					break;
				case 'S':
				case 's': t = TimeUnit.SECONDS.toNanos(i);
					break;
				default:
					System.err.println("无效的时间单位");
					return;
			}
			userMuteMap.put(userName, System.nanoTime() + t);
		}
	}

	public void updateMuteStatus() {
		userMuteMap.forEach(((name, muteInfo) -> {
			if (muteInfo <= System.nanoTime()) userMuteMap.remove(name);
		}));
	}

	public boolean isUserMuted(String userName) {
		updateMuteStatus();
		return userMuteMap.get(userName) != null;
	}

	public void banUser(String userName) {
		if (!bannedUserList.contains(userName)) {
			bannedUserList.add(userName);
			if (userMap.containsKey(userName)) userMap.remove(userName).close();
			System.out.println("用户[" + userName + "]已被禁止加入服务器!");
		} else {
			bannedUserList.remove(userName);
			System.out.println("用户[" + userName + "]已被从服务器黑名单中移除!");
		}
	}

	public void banIp(String address) {
		if (!bannedIpList.contains(address)) {
			bannedIpList.add(address);
			userMap.forEach((key, value) -> {
				if (value.getClientIp().equals(address)) userMap.remove(key).close();
			});
			System.out.println("ip地址[" + address + "]已被禁止加入服务器!");
		} else {
			bannedIpList.remove(address);
			System.out.println("ip地址[" + address + "]已被从服务器黑名单中移除!");
		}
	}

	public void kick(String userName) {
		if (userMap.containsKey(userName)) userMap.remove(userName).close();
		else System.err.println("该用户不存在!");
	}

	public boolean isUserBanned(String userName) {
		return bannedUserList.contains(userName);
	}

	public boolean isIpBanned(String address) {
		return bannedIpList.contains(address);
	}

	public void forEach(Consumer<SocketServer.ClientHandler> action) {
		userMap.forEach((s, clientHandler) -> action.accept(clientHandler));
	}

	public int getUserCount() {
		return userMap.size();
	}

}
